home *** CD-ROM | disk | FTP | other *** search
/ Light ROM 1 / LIGHT-ROM 1 (Amiga Library Services)(1994).iso / ffdisks / d910.lha / DSound / PlayMono2.c < prev    next >
C/C++ Source or Header  |  1993-08-28  |  5KB  |  198 lines

  1.  
  2. /*************************************************************************/
  3. /*                  PlayMono2.c                 */
  4. /*     Contains code used to play mono samples out of both speakers.     */
  5. /*************************************************************************/
  6.  
  7. #include <exec/types.h>
  8. #include <exec/exec.h>
  9. #include <devices/audio.h>
  10. #include <dos/dos.h>
  11. #include <intuition/intuition.h>
  12. #include <intuition/intuitionbase.h>
  13. #include <graphics/gfxbase.h>
  14. #include <stdlib.h>
  15.  
  16. #include "dsound.h"
  17.  
  18. #include <proto/intuition.h>
  19. #include <proto/exec.h>
  20. #include <proto/dos.h>
  21.  
  22. extern UBYTE rightAMap[];
  23. extern UBYTE leftAMap[];
  24. extern UBYTE eitherAMap[];
  25. extern UBYTE bothAMap[];
  26.  
  27. extern UBYTE volume;
  28. extern UWORD speed;
  29. extern ULONG bufSize;
  30.  
  31. extern BOOL readAll;
  32. extern BOOL loop;
  33. extern struct Window *window;
  34.  
  35. extern ULONG signalMask;
  36.  
  37. /*Play a mono sample (or a single channel of a stereo sample) out of */
  38. /*both speakers simultaneously*/
  39. void playMonoTwice(BPTR file,channel audioChannel,struct Voice8Header *vhdr,
  40.            ULONG length)
  41. {
  42.    struct IOAudio *iob1_right,*iob2_right,*iob1_left,*iob2_left;
  43.    struct IOAudio *cur_right,*cur_left,*alt_right,*alt_left;
  44.    ULONG toRead;
  45.    ULONG sampleSize=length;
  46.    BOOL done=FALSE;
  47.    ULONG amountLeft;
  48.  
  49.    /*Read the entire sample into memory, if the user so specified*/
  50.    if(readAll)
  51.    {
  52.       storeLeft(file,length,bufSize);
  53.       file=0L;
  54.    }
  55.  
  56.    /*Get the first audio channel*/
  57.    iob1_left=GetAudioChannel(bufSize,leftAMap);
  58.    if(iob1_left==NULL)
  59.    {
  60.       WriteMsg("Couldn't create the first stereo buffer\n");
  61.       cleanup(150);
  62.    }
  63.  
  64.    iob1_right=GetAudioChannel(bufSize,rightAMap);
  65.    if(iob1_right==NULL)
  66.    {
  67.       WriteMsg("Couldn't create the second stereo buffer\n");
  68.       cleanup(150);
  69.    }
  70.    FreeMem(iob1_right->ioa_Data,iob1_right->ioa_Length);
  71.    iob1_right->ioa_Data=iob1_left->ioa_Data;
  72.  
  73.    /* If the user didn't specify a volume, get it from the VHDR */
  74.    if(volume==0)
  75.       volume=(vhdr->volume>>10);
  76.  
  77.    /* If the VHDR gave a volume of zero, use maximum volume*/
  78.    if(volume==0)
  79.       volume=64;
  80.  
  81.    /* Get the samples/sec rate (either the rate given by the user, or the*/
  82.    /* rate found in the VHDR) */
  83.    if(speed==0)
  84.       speed=1000000000/(vhdr->samplesPerSec*279);
  85.    else
  86.       speed=1000000000/(speed*279);
  87.  
  88.    InitAudioChannel(iob1_left,volume,speed);
  89.    InitAudioChannel(iob1_right,volume,speed);
  90.  
  91.    /*Get the 2nd audio channel*/
  92.    iob2_left=DuplicateAudioChannel(iob1_left);
  93.  
  94.    if(iob2_left==NULL)
  95.    {
  96.       FreeAudioChannel(iob1_left);
  97.       FreeAudioChannel(iob1_right);
  98.       WriteMsg("Couldn't create the second buffer");
  99.       cleanup(175);
  100.    }
  101.  
  102.    iob2_right=DuplicateAudioChannel(iob1_right);
  103.    if(iob2_right==NULL)
  104.    {
  105.       FreeAudioChannel(iob1_left);
  106.       DeleteDuplication(iob2_left);
  107.       FreeAudioChannel(iob1_right);
  108.       WriteMsg("Couldn't create the second buffer");
  109.       cleanup(175);
  110.    }
  111.    FreeMem(iob2_right->ioa_Data,iob2_right->ioa_Length);
  112.    iob2_right->ioa_Data=iob2_left->ioa_Data;
  113.  
  114.    /* Load the first buffer*/
  115.    toRead=MIN(length,bufSize);
  116.    LoadAudioBuffer(file,iob1_left,toRead);
  117.    iob1_right->ioa_Length=iob1_left->ioa_Length=toRead;
  118.    length-=toRead;
  119.  
  120.    if(length==0 && loop)
  121.    {
  122.       length=sampleSize;
  123.       if(!readAll)
  124.      Seek(file,-sampleSize,OFFSET_CURRENT);
  125.    }
  126.  
  127.    /*Initialize the sample position info*/
  128.    updateSampleInfo(0,sampleSize,vhdr->samplesPerSec);
  129.  
  130.    /*Queue up the play requests*/
  131.    BeginIO((struct IORequest *)iob1_left);
  132.    BeginIO((struct IORequest *)iob1_right);
  133.  
  134.    cur_right=iob2_right;
  135.    cur_left=iob2_left;
  136.    alt_right=iob1_right;
  137.    alt_left=iob1_left;
  138.  
  139.    /*Loop while there's stuff to read*/
  140.    while(!done)
  141.    {
  142.       toRead=MIN(length,bufSize);
  143.  
  144.       if(toRead!=0)
  145.       {
  146.      LoadAudioBuffer(file,cur_left,toRead);
  147.      cur_right->ioa_Length=cur_left->ioa_Length=toRead;
  148.      BeginIO((struct IORequest *)cur_left);
  149.      BeginIO((struct IORequest *)cur_right);
  150.      amountLeft=length-=toRead;
  151.  
  152.      if(length==0 && loop)
  153.      {
  154.         length=sampleSize;
  155.         if(!readAll)
  156.            Seek(file,-sampleSize,OFFSET_CURRENT);
  157.      }
  158.      done=FALSE;
  159.       }
  160.       else
  161.      done=TRUE;
  162.  
  163.       /*Wait for the second buffer to finish*/
  164.       if((Wait((1<<alt_right->ioa_Request.io_Message.mn_ReplyPort->mp_SigBit) |
  165.        signalMask) & SIGBREAKF_CTRL_C) == SIGBREAKF_CTRL_C)
  166.      done=TRUE;
  167.  
  168.       /*Update the sample position info*/
  169.       updateSampleInfo(sampleSize-amountLeft-toRead,sampleSize,vhdr->samplesPerSec);
  170.  
  171.       /*If we got a message from the window, it is a CLOSEWINDOW message*/
  172.       /*and we're done*/
  173.       if(window!=NULL && GetMsg(window->UserPort)!=NULL)
  174.       {
  175.      done=TRUE;
  176.       }
  177.       swapPointers(&cur_left,&alt_left);
  178.       swapPointers(&cur_right,&alt_right);
  179.    }
  180.  
  181.    /*Restore the buffer lengths, so that FreeAudio() channel, etc., knows*/
  182.    /*how much memory to free*/
  183.    iob1_left->ioa_Length=iob2_left->ioa_Length=bufSize;
  184.    iob1_right->ioa_Length=iob2_right->ioa_Length=bufSize;
  185.  
  186.    iob1_right->ioa_Data=NULL;
  187.    iob2_right->ioa_Data=NULL;
  188.  
  189.    FreeAudioChannel(iob1_left);
  190.    DeleteDuplication(iob2_left);
  191.    FreeAudioChannel(iob1_right);
  192.    DeleteDuplication(iob2_right);
  193.  
  194.    return;
  195. }
  196.  
  197. /*End of Play.c*/
  198.